package com.zjdiepu.www.manage.api.dh;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import java.util.Arrays;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.zjdiepu.www.base.BaseController;
import com.zjdiepu.www.base.annotation.Add;
import com.zjdiepu.www.base.annotation.Update;
import com.zjdiepu.www.base.core.AbstractSysUser;
import com.zjdiepu.www.base.core.ApiResponse;
import com.zjdiepu.www.base.core.PageVo;
import com.zjdiepu.www.base.enums.status.DpStatus;
import com.zjdiepu.www.base.enums.status.InsteadPaymentStatus;
import com.zjdiepu.www.base.enums.type.DataDicType;
import com.zjdiepu.www.base.enums.type.ResultFlagType;
import com.zjdiepu.www.base.enums.type.SortFieldType;
import com.zjdiepu.www.common.util.CollectionInitUtil;
import com.zjdiepu.www.common.util.UUIDFactory;
import com.zjdiepu.www.commpent.ViewScopeControllerHelper;
import com.zjdiepu.www.ibiz.base.IDpBaseDealerInfoService;
import com.zjdiepu.www.ibiz.cache.ICachedDictionaryService;
import com.zjdiepu.www.ibiz.dh.IDhInsteadPaymentApplyService;
import com.zjdiepu.www.ibiz.dh.IDhInsteadPaymentService;
import com.zjdiepu.www.ibiz.fk.IFkAuditListService;
import com.zjdiepu.www.ibiz.system.IBaseBusiUnitService;
import com.zjdiepu.www.ibiz.system.IBaseSalersmanService;
import com.zjdiepu.www.ibiz.yw.IYwBusinessService;
import com.zjdiepu.www.model.base.dto.SysUser;
import com.zjdiepu.www.model.dh.dto.DhInsteadPaymentApply;
import com.zjdiepu.www.model.dh.ext.DhInsteadPaymentApplyExt;
import com.zjdiepu.www.model.dh.vo.DhInsteadPaymentApplyVo;
import com.zjdiepu.www.model.yw.dto.YwBusiness;
import com.zjdiepu.www.model.yw.request.YwBusinessQueryRequest;
import com.zjdiepu.www.util.YwExtHelper;

/**
 * Controller层<br/>
 * @author zhangzhengjian<br/>
 * @time 2017年05月22日 20时55分<br/>
 * @since 1.0<br/>
 */
@RestController
@RequestMapping(value= "DhInsteadPaymentApplyController", produces="application/json;charset=UTF-8")
@Api(tags = {""}, produces="application/json;charset=UTF-8")
public class DhInsteadPaymentApplyController extends BaseController {
    @Resource
    private IDhInsteadPaymentApplyService dhInsteadPaymentApplyService;
    @Resource
    private IDhInsteadPaymentService dhInsteadPaymentService;
    @Resource
    private ViewScopeControllerHelper viewScopeControllerHelper;
    @Resource
    private ICachedDictionaryService cachedDictionaryService;
    @Resource
    private IFkAuditListService fkAuditListService;
    @Resource
    private IYwBusinessService ywBusinessService;
    @Resource
    private IBaseSalersmanService baseSalersmanService;
    @Resource
    private IBaseBusiUnitService baseBusiUnitService;
    @Resource
    private IDpBaseDealerInfoService baseDealerInfoService;
    
    
    
    @ApiOperation(value = "条件查询业务信息",response = YwBusiness.class, httpMethod="POST", tags={"多表查询"})
    @RequestMapping(value = "multiList", method = RequestMethod.POST)
    public ApiResponse multiList(@Validated @ModelAttribute YwBusinessQueryRequest ywBusinessQueryRequest){
    	SysUser currentUser = (SysUser)getCurrentUser();
        PageVo page = getPage(ywBusinessQueryRequest);
        ywBusinessQueryRequest.setBaseWhere();
        ywBusinessQueryRequest.setBankLoan(currentUser.getLoanBank());
        ywBusinessQueryRequest.setCodeUnits( currentUser.getCodeUnits());
        ywBusinessQueryRequest.setWhereSql(" AND D.STATUS_INSTEAD <=25 ");
        YwExtHelper.initExt(ywBusinessQueryRequest, SortFieldType.SUBMIT_TIME);
        int records = dhInsteadPaymentApplyService.countMultiByExt(ywBusinessQueryRequest);
        page.setRecords(records);
        if (records > 0) {
            List<DhInsteadPaymentApplyVo> ywBusinesss = dhInsteadPaymentApplyService.selectMultiByExt(ywBusinessQueryRequest);
            for (DhInsteadPaymentApplyVo ywBusiness : ywBusinesss) {
            	ywBusiness.setLoanBankDisplay(cachedDictionaryService.getName(String.valueOf(ywBusiness.getLoanBank()), DataDicType.LOAN_BANK));
			}
            page.setRows(ywBusinesss);
        } else {
            page.setRows(CollectionInitUtil.emptyList());
        }
        return ApiResponse.success(page);
    }
    @ApiOperation(value = "条件查询业务信息",response = YwBusiness.class, httpMethod="POST", tags={"多表查询"})
    @RequestMapping(value = "kehuMultiList", method = RequestMethod.POST)
    public ApiResponse kehuMultiList(@Validated @ModelAttribute YwBusinessQueryRequest ywBusinessQueryRequest){
    	SysUser currentUser = (SysUser)getCurrentUser();
        PageVo page = getPage(ywBusinessQueryRequest);
        ywBusinessQueryRequest.setBaseWhere();
        ywBusinessQueryRequest.setBankLoan(currentUser.getLoanBank());
        ywBusinessQueryRequest.setCodeUnits( currentUser.getCodeUnits());
        ywBusinessQueryRequest.setStatusInstead(InsteadPaymentStatus.YESJIHE.getCode());
        YwExtHelper.initExt(ywBusinessQueryRequest, SortFieldType.SUBMIT_TIME);
        int records = dhInsteadPaymentApplyService.countMultiByExt(ywBusinessQueryRequest);
        page.setRecords(records);
        if (records > 0) {
            List<DhInsteadPaymentApplyVo> ywBusinesss = dhInsteadPaymentApplyService.selectMultiByExt(ywBusinessQueryRequest);
            for (DhInsteadPaymentApplyVo ywBusiness : ywBusinesss) {
            	ywBusiness.setLoanBankDisplay(cachedDictionaryService.getName(String.valueOf(ywBusiness.getLoanBank()), DataDicType.LOAN_BANK));
			}
            page.setRows(ywBusinesss);
        } else {
            page.setRows(CollectionInitUtil.emptyList());
        }
        return ApiResponse.success(page);
    }
    /**
     * @author zhangzhengjian <br/>
     * @Title: save  <br/>
     * @Description: 新增 <br/>
     * @param dhInsteadPaymentApply
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "新增", response = Boolean.class, httpMethod="POST", tags={"新增"})
    @RequestMapping(value = "save", method = RequestMethod.POST)
    public ApiResponse save(@Validated(Add.class) @ModelAttribute DhInsteadPaymentApply dhInsteadPaymentApply) {
    	AbstractSysUser currentUser = getCurrentUser();
    	dhInsteadPaymentApply.setModifier(currentUser.getId()); //统一使用修改人和修改人名称来初始化
    	dhInsteadPaymentApply.setModifierName(currentUser.getName());
//    	dhInsteadPaymentApply.setStatusInstead(InsteadPaymentStatus.YESAPPLY.getCode());
    	
    	dhInsteadPaymentApplyService.saveAll(dhInsteadPaymentApply,currentUser);
        return ApiResponse.success(true);
    }

    /**
     * @author zhangzhengjian <br/>
     * @Title: remove  <br/>
     * @Description: 删除<br/>
     * @param id
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "删除", response = Boolean.class, httpMethod="POST", tags={"删除"})
    @ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "数据主键", paramType="query", required = true, example="1")
    })
    @RequestMapping(value = "remove", method = RequestMethod.POST)
    public ApiResponse remove(String id) {
    	if(StringUtils.isBlank(id)) {
    		return ApiResponse.fail(ResultFlagType.PARAM_EMPTY);
    	}
    	AbstractSysUser currentUser = getCurrentUser();
    	List<String> ids = Arrays.asList(id.split(","));
    	dhInsteadPaymentApplyService.updateBatch(currentUser.getId(), currentUser.getName(), DpStatus.DELETED.getCode(), ids);
        return ApiResponse.success(true);
    }
    
    /**
     * @author zhangzhengjian <br/>
     * @Title: disable  <br/>
     * @Description: 禁用<br/>
     * @param id
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "禁用", response = Boolean.class, httpMethod="POST", tags={"禁用"})
    @ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "数据主键", paramType="query", required = true, example="1")
    })
    @RequestMapping(value = "disable", method = RequestMethod.POST)
    public ApiResponse disable(String id) {
    	if(StringUtils.isBlank(id)) {
    		return ApiResponse.fail(ResultFlagType.PARAM_EMPTY);
    	}
    	AbstractSysUser currentUser = getCurrentUser();
    	List<String> ids = Arrays.asList(id.split(","));
    	dhInsteadPaymentApplyService.updateBatch(currentUser.getId(), currentUser.getName(), DpStatus.INVALID.getCode(), ids);
        return ApiResponse.success(true);
    }
    
    /**
     * @author zhangzhengjian <br/>
     * @Title: enable <br/>
     * @Description: 启用<br/>
     * @param id
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "启用", response = Boolean.class, httpMethod="POST", tags={"启用"})
    @ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "数据主键", paramType="query", required = true, example="1")
    })
    @RequestMapping(value = "enable", method = RequestMethod.POST)
    public ApiResponse enable(String id) {
    	if(StringUtils.isBlank(id)) {
    		return ApiResponse.fail(ResultFlagType.PARAM_EMPTY);
    	}
    	AbstractSysUser currentUser = getCurrentUser();
    	List<String> ids = Arrays.asList(id.split(","));
    	dhInsteadPaymentApplyService.updateBatch(currentUser.getId(), currentUser.getName(), DpStatus.NORMAL.getCode(), ids);
        return ApiResponse.success(true);
    }

    /**
     * @author zhangzhengjian <br/>
     * @Title: modify  <br/>
     * @Description: 修改 <br/>
     * @param dhInsteadPaymentApply
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "修改", response = Boolean.class, httpMethod="POST", tags={"修改"})
    @RequestMapping(value = "modify", method = RequestMethod.POST)
    public ApiResponse modify(@Validated(Update.class) @ModelAttribute DhInsteadPaymentApply dhInsteadPaymentApply) {
    	AbstractSysUser currentUser = getCurrentUser();
    	dhInsteadPaymentApply.setModifier(currentUser.getId()); //统一使用修改人和修改人名称来初始化
    	dhInsteadPaymentApply.setModifierName(currentUser.getName());
    	dhInsteadPaymentApply.setStatusInstead(InsteadPaymentStatus.YESAPPLY.getCode());
    	dhInsteadPaymentApplyService.modifyAll(dhInsteadPaymentApply);
        return ApiResponse.success(true);
    }

    /**
     * @author zhangzhengjian <br/>
     * @Title: getById  <br/>
     * @Description: 主键查询<br/>
     * @param id
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "主键查询", response = DhInsteadPaymentApply.class, httpMethod="POST", tags={"主键查询"})
    @ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "数据主键", paramType="query", required = true, example="1")
    })
    @RequestMapping(value = "getById", method = RequestMethod.POST)
    public ApiResponse getById(String id){
    	if(StringUtils.isBlank(id)) {
    		return ApiResponse.fail(ResultFlagType.PARAM_EMPTY);
    	}
       DhInsteadPaymentApply dhInsteadPaymentApply = dhInsteadPaymentApplyService.selectById(id);
       if(null == dhInsteadPaymentApply) {
    	   return ApiResponse.fail(ResultFlagType.MISSING);
       }
       return ApiResponse.success(dhInsteadPaymentApply);
    }
    
    /**
     * @author zhangzhengjian <br/>
     * @Title: list  <br/>
     * @Description: 条件查询 <br/>
     * @param dhInsteadPaymentApplyExt
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "条件查询",response = DhInsteadPaymentApply.class, httpMethod="POST", tags={"列表"})
    @RequestMapping(value = "list", method = RequestMethod.POST)
    public ApiResponse list(@ModelAttribute DhInsteadPaymentApplyExt dhInsteadPaymentApplyExt){
        dhInsteadPaymentApplyExt.notDeleted();
        PageVo page = getPage(dhInsteadPaymentApplyExt);
        int records = dhInsteadPaymentApplyService.countByExt(dhInsteadPaymentApplyExt);
        page.setRecords(records);
        if (records > 0) {
            List<DhInsteadPaymentApply> dpBaseDataDictionaries = dhInsteadPaymentApplyService.selectByExt(dhInsteadPaymentApplyExt);
            page.setRows(dpBaseDataDictionaries);
        } else {
            page.setRows(CollectionInitUtil.emptyList());
        }
        return ApiResponse.success(page);
    }
    
    @ApiOperation(value = "条件查询",response = DhInsteadPaymentApply.class, httpMethod="POST", tags={"列表"})
    @RequestMapping(value = "listWithoutPage", method = RequestMethod.POST)
    public ApiResponse listWithoutPage(@ModelAttribute DhInsteadPaymentApplyExt dhInsteadPaymentApplyExt){
        dhInsteadPaymentApplyExt.notDeleted();
        dhInsteadPaymentApplyExt.withOutPage();
        PageVo page = getPage(dhInsteadPaymentApplyExt);
        List<DhInsteadPaymentApply>  list= dhInsteadPaymentApplyService.selectByExt(dhInsteadPaymentApplyExt);
        page.setRows(list);
        return ApiResponse.success(page);
    }
    /**
     * 客户代偿签字附件
     * @param model
     * @return
     */
    @RequestMapping(value = "custCompensatoryAttch", method = RequestMethod.POST)
    public ApiResponse custCompensatoryAttch(Model model) {
    	
		//判断条件
    	viewScopeControllerHelper.preparedAttachmentEnum(model);
		model.addAttribute("uuid", UUIDFactory.getUUID());
		
    	return ApiResponse.success(model);
    }
    @ApiOperation(value = "修改撤回状态",response = DhInsteadPaymentApply.class, httpMethod="POST", tags={"列表"})
    @RequestMapping(value = "revokeInsteadPayment", method = RequestMethod.POST)
    public ApiResponse revokeInsteadPayment(@Validated(Update.class) @ModelAttribute DhInsteadPaymentApply dhInsteadPaymentApply){
    	
    	AbstractSysUser currentUser = getCurrentUser();
    	dhInsteadPaymentApply.setModifier(currentUser.getId()); //统一使用修改人和修改人名称来初始化
    	dhInsteadPaymentApply.setModifierName(currentUser.getName());
    	dhInsteadPaymentApplyService.revokeInsteadPayment(dhInsteadPaymentApply,currentUser);
    	return ApiResponse.success(true);
    }
}
